package com.tanhua.app.Service;

import com.tanhua.app.exception.BusinessException;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.dubbo.api.SoulReportApi;
import com.tanhua.dubbo.api.SoulTestApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.enums.ReportType;
import com.tanhua.model.mongo.SoulReport;
import com.tanhua.model.pojo.Answers;
import com.tanhua.model.pojo.Dimensions;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.SoulReportVo;
import com.tanhua.model.vo.SoulVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SoulService {
    @DubboReference
    private SoulTestApi soulTestApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private SoulReportApi soulReportApi;

    //    测灵魂-问卷列表（学生实战)
    public List<SoulVo> getTest() {
        Long userId = UserHolder.getUserId();
        UserInfo userInfo = userInfoApi.findById(userId);
        //获取当前用户目前的问卷级别
        Integer level = userInfo.getLevel();
        //设置问卷级别默认值
        if (level == null) {
            level = 1;
        }
        //查询结果
        ObjectId soulReportId = soulReportApi.findNewReport(userId);
        //查询试卷
        List<SoulVo> vos = soulTestApi.findTest();
        for (SoulVo vo : vos) {
            Integer l = Integer.parseInt(vo.getLevel());
            if (level < l) {
                vo.setIsLock(1);
            }
            if (soulReportId != null) {
                vo.setReportId(soulReportId.toString());
            }
            if(l==1){
                vo.setLevel("初级");
            }else if (l == 2) {
                vo.setLevel("中级");
            }else if (l == 3) {
                vo.setLevel("高级");
            }
        }
        return vos;
    }

    //    测灵魂-提交问卷（学生实战）
    public String getAnswer(List<Answers> answers) {
        List<String> optionId = answers.stream().map(Answers::getOptionId).collect(Collectors.toList());
        Integer allScore = soulTestApi.getScore(optionId);

        SoulReport soulReport = new SoulReport();
        soulReport.setScore(allScore);
        soulReport.setCreated(System.currentTimeMillis());
        soulReport.setUserId(UserHolder.getUserId());
        if (allScore < 20) {
            soulReport.setConclusion(ReportType.getConclusion(1));
            soulReport.setCover(ReportType.getCover(1));
        } else if (allScore > 40) {
            soulReport.setConclusion(ReportType.getConclusion(2));
            soulReport.setCover(ReportType.getCover(2));
        } else {
            soulReport.setConclusion(ReportType.getConclusion(3));
            soulReport.setCover(ReportType.getCover(3));
        }

        //纬度添加
//        外向,判断,抽象,理性
        List<Dimensions> list = new ArrayList<>();
        Dimensions dim = new Dimensions("外向", "80");
        list.add(dim);
        dim = new Dimensions("判断","70");
        list.add(dim);
        dim = new Dimensions("抽象","60");
        list.add(dim);
        dim = new Dimensions("理性","50");
        list.add(dim);
        soulReport.setDimensions(list);
        ObjectId id = soulReportApi.save(soulReport);

        userInfoApi.levelUp(UserHolder.getUserId());
        return id.toString();
    }

    //    测灵魂-查看结果（学生实战）
    public SoulReportVo findReport(String id) {
        SoulReport sr = soulReportApi.findById(id);
        List<Long> ids = soulReportApi.findFriend(id);
        List<UserInfo> userInfos = userInfoApi.findByUserIds(ids);
        if (sr == null) {
            throw new BusinessException(ErrorResult.soulReportError());
        }
        SoulReportVo vo = SoulReportVo.init(userInfos, sr);
        return vo;
    }
}
